package cn.billy.org.service.impl;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.query.BaseQuery;
import cn.billy.basic.utils.BaiduAuditUtils;
import cn.billy.basic.utils.PageList;
import cn.billy.org.domain.Employee;
import cn.billy.org.domain.Shop;
import cn.billy.org.domain.ShopAuditLog;
import cn.billy.org.mapper.EmployeeMapper;
import cn.billy.org.mapper.ShopAuditLogMapper;
import cn.billy.org.mapper.ShopMapper;
import cn.billy.org.service.IEmployeeService;
import cn.billy.org.service.IShopService;
import cn.billy.basic.service.impl.BaseServiceImpl;
import cn.billy.org.vo.ShopVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author billy
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Override
    public void settlement(Shop shop) {
        System.out.println("3333333333"+shop);
        // 使用string工具类的isEmpty判断店铺名字是否为空
        if (StringUtils.isEmpty(shop.getName())){
           throw new BusinessException("店铺名称不能为空，请重新输入");
        }
        // 查看数据库店铺名是否重复
        Shop loadName = shopMapper.loadByName(shop.getName());
        if(loadName != null){
            throw new BusinessException("店铺名已存在，请重新输入");
        }

        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
            throw new BusinessException("店铺名存在敏感词汇，请重新输入");
        }
        Boolean aBoolean1 = BaiduAuditUtils.TextCensor(shop.getAdmin().getUsername());
        if (!aBoolean1){
            throw new BusinessException("管理员名字存在敏感词汇，请重新输入");
        }
//        String str = "http://123.207.27.208";
//        Boolean imgCensor = BaiduAuditUtils.ImgCensor(str + shop.getLogo());
//        if (imgCensor){
//            throw new BusinessException("图片不合规请重新选取");
//        }
        // 查看店铺地址是否重复
        Shop loadAddress = shopMapper.loadByAddress(shop.getAddress());
        if (loadAddress != null){
            throw new BusinessException("所填地址以有店铺，请重新输入");
        }
        // 获取shop集合里面的admin对象，然后获取名字看看是否有这个员工，然后再看员工是否已绑定店铺
        Employee admin = shop.getAdmin();
        Employee loadUsername = employeeMapper.loadByUsername(admin.getUsername());

        if (loadUsername != null && loadUsername.getShopId() != null){
            throw new BusinessException("此员工以绑定店铺，请重新输入");
        }
        // 如果等于空就说明没有这个新员工，就直接将shop里面的admin对象添加进去
        if (loadUsername == null){
            iEmployeeService.save(admin);
            // 这一步是为了方便直接就用一个对象
            loadUsername = admin;
        }
        // 将员工的id赋值到shop里面的adminId中
        shop.setAdminId(loadUsername.getId());
        shopMapper.add(shop);
        // 将店铺的id赋值到ShopId中
        loadUsername.setShopId(shop.getId());
        employeeMapper.update(loadUsername);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {

        Long shopId = Long.valueOf(shopAuditLog.getShopId());
        Shop byId = shopMapper.getById(shopId);
        byId.setState(-1);
        shopMapper.update(byId);
        shopAuditLog.setAuditId(334);


        shopAuditLogMapper.add(shopAuditLog);
            try {
                // 1.创建复杂邮件对象
                MimeMessage mimeMessage = javaMailSender.createMimeMessage();
                // 2.发送复杂邮件的工具类
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
                helper.setFrom("1252904712@qq.com");
                helper.setSubject("测试复杂邮件");
                // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
                helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为内容不合规被驳回，请修改店铺信息后，再次申请</h1>"+
                        "店铺信息修改链接：<a href='http://localhost:8081/#/Register2?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>",true);
                // 4.添加附件
                helper.addAttachment("好东西.jpg",new File("C:\\Users\\asus\\Desktop\\2.jpg"));
                // 5.设置收件人
                helper.setTo("1252904712@qq.com");
                // 6.发送邮件
                javaMailSender.send(mimeMessage);
            } catch (MessagingException e){
                e.printStackTrace();
            }

    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        Long shopId = Long.valueOf(shopAuditLog.getShopId());

        Shop byId = shopMapper.getById(shopId);
        byId.setState(1);
        shopMapper.update(byId);
        shopAuditLog.setAuditId(334);


        shopAuditLogMapper.add(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1252904712@qq.com");
            helper.setSubject("测试复杂邮件");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息在平台已通过审核，请尽快激活</h1>"+
                    "店铺激活链接：<a href='http://localhost:8080/shop/"+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\asus\\Desktop\\2.jpg"));
            // 5.设置收件人
            helper.setTo("1252904712@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e){
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> loadShop() {
        List<ShopVo> shopVos = shopMapper.loadShop();
        List<Integer> x = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> y = shopVos.stream().map(ShopVo::getCount).collect(Collectors.toList());

        HashMap<String, Object> data = new HashMap<>();
        data.put("x",x);
        data.put("y",y);
        return data;
    }

    @Override
    public void fail(ShopAuditLog shopAuditLog) {
        Long shopId = Long.valueOf(shopAuditLog.getShopId());

        Shop byId = shopMapper.getById(shopId);
        byId.setState(-2);
        shopMapper.update(byId);
        // 设置操作人
        shopAuditLog.setAuditId(334);
        shopAuditLogMapper.add(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1252904712@qq.com");
            helper.setSubject("测试复杂邮件");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，您的店铺入驻申请信息我平台已审核，但是因为您信誉不好，没有资格在我们商城入驻，我们有缘在见</h1>"
                    ,true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\asus\\Desktop\\2.jpg"));
            // 5.设置收件人
            helper.setTo("1252904712@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
    @Override
    public PageList<Shop> queryData(BaseQuery baseQuery) {
        Integer total = shopMapper.queryCount(baseQuery);
        List<Shop> rows = shopMapper.queryData(baseQuery);

        System.out.println(rows);
        for (Shop s : rows){
            ShopAuditLog byId = shopAuditLogMapper.getById(s.getId());

        }
        return new PageList<>(total,rows);
    }

}

